package duylinh

class DuyLinhTagLib {

    def utilsService

    static namespace = "dl"


    def getSanPhamPhoto = {attrs, body->
        def sanPham = attrs.sanPham
        out<< utilsService.URL_TO_DUYLINH_STORAGE_SANPHAM_FS + "/sanPham_"+sanPham.id + "/original.jpg"
    }



    def getVatLieuPhoto = {attrs, body->
        def vatLieu = attrs.vatLieu
        def getThumb = attrs.getThumb

        if (vatLieu && getThumb == true)
        {
            out<< utilsService.URL_TO_DUYLINH_STORAGE_VATLIEU_FS + "/vatLieu_"+vatLieu?.id + "/thumbnail.jpg"
        }
        else
        {
            out<< utilsService.URL_TO_DUYLINH_STORAGE_VATLIEU_FS + "/vatLieu_"+vatLieu?.id + "/original.jpg"
        }
    }


    /*
   uploadify - Super jQuery Flash-based Upload plugin. For more details visit http://www.uploadify.com/

   Example Usage:

   <sng:uploadify
       name="postSymbolUpload"
       action="../post/saveTempPostSymbol"
       label="${message(code:'label.ajaxUpload')}"
       fileExt="*.jpg; *.png; *.gif"
       sizeLimit="${500*1024}"
       onComplete="myOnComplete(response)"
       >
   </sng:uploadify>
    */
    def uploadify = {attrs, body ->
        def name = attrs.name // unique name to use on the client side. Required!
        def action = attrs.action // the action processing file upload request. Required!

        def params = attrs.params //
        def label = attrs.label // descriptive label for the upload.
        def multi = attrs.multi // true/false - if set to 'true' allow multiple file upload. Default set to 'false'.
        def auto = attrs.auto // true/false - if set to 'true' the file will be automaticaly uploaded after selection. Default set to 'true'.
        def fileExt = attrs.fileExt // A list of file extensions you would like to allow for upload. Format like '*.ext1;*. ext2;*.ext3'.
        def sizeLimit = attrs.sizeLimit // A number representing the limit in bytes for each upload.
        def simUploadLimit = attrs.simUploadLimit // A number of simultanous uploads allowed at the same time. Default set to 1.
        def fileDesc = attrs.fileDesc // The text that will appear in the file type drop down at the bottom of the browse dialog box.
        def delay = attrs.delay // in ms, delays the execution of complete callback. OPTIONAL.

        // JS callbacks
        def onSelect = attrs.onSelect
        def onSelectOnce = attrs.onSelectOnce
        def onCancel = attrs.onCancel
        def onClearQueue = attrs.onClearQueue
        def onError = attrs.onError
        def onProgress = attrs.onProgress
        def onComplete = attrs.onComplete // Called for each complet upload. Input parameter 'response' is the server side return.
        def onAllComplete = attrs.onAllComplete

        def styleSnip = ""
        if(attrs.style) {
            styleSnip = "style='${attrs.style}'"
        }

        def paramsSnip = "{"
        params.each {
            paramsSnip += "${it.key}:'${it.value}', "
        }
        paramsSnip += "dummy:'dummy'}"

        def fileExtSnip = ""
        if(fileExt) fileExtSnip = "'fileExt':'${fileExt}', \n"

        def sizeLimitSnip = ""
        if(sizeLimit) sizeLimitSnip = "'sizeLimit':${sizeLimit}, \n"

        def simUploadLimitSnip = "'simUploadLimit':1, \n"
        if(simUploadLimit) simUploadLimitSnip = "'simUploadLimit':${simUploadLimit}, \n"

        def fileDescSnip = "'fileDesc':'${fileExt}', \n"
        if(fileDesc) fileDescSnip = "'fileDesc':'${fileDesc}', \n"

        out << "<div class='uploadify  ${attrs['class']?attrs['class']:''}' ${styleSnip}>"
        out << "<span class='uploadifyLabel'>${label}</span>"
        out << "<span id='${name}'></span>"

        def initSnip = "var ${name};"
        if(attrs.inBoxy) {
            initSnip += """
            setTimeout('initUploadify_$name()',10);

            function initUploadify_$name() {
          """
        }
        else {
            initSnip += """
            \$(function() {
          """
        }

        def snip = """
          <script type="text/javascript">
              ${initSnip}
                ${name} = \$('#${name}').uploadify({
                    'uploader':'${request.contextPath}/js/jquery/uploadify/uploadify.swf',
                    'script':'${action}',
                    'scriptData':${paramsSnip},
                    'cancelImg':'${request.contextPath}/js/jquery/uploadify/cancel.png',
                    'folder':'uploads',
                    'auto':${auto?auto:'true'},
                    'multi':${multi?multi:'true'},
                    'fileDataName':'file',
                    'wmode':'transparent',
                    'width':'66',
                    'height': '18',
                    'buttonImg':'${request.contextPath}/js/jquery/uploadify/buttonImg.png',
                    ${fileExtSnip}
                    ${sizeLimitSnip}
                    ${simUploadLimitSnip}
                    ${fileDescSnip}

                    onSelect: function(event, qId, fileObj) {${onSelect?onSelect:''}},
                    onSelectOnce: function(event, qData) {${onSelectOnce?onSelectOnce:''}},
                    onCancel: function(event, qId, fileObj, qData) {${onCancel?onCancel:''}},
                    onClearQueue: function(event, qData) {${onClearQueue?onClearQueue:''}},
                    onError: function(event, qId, fileObj, errorObj) {

                        if(errorObj.type == 'Security') {
                            alert(errorObj.type + ' - ' + errorObj.info);
                        }
                        else if(errorObj.type == 'IO') {
                            alert(errorObj.type + ' - ' + errorObj.info);
                        }
                        else if(errorObj.type == 'File Size') {
                            alert("The file " + fileObj.name + " is too large ("+fileObj.size+" bytes). Maximal upload file size allowed is $sizeLimit bytes");
                        }
                        else {
                            alert(errorObj.type + ' - ' + errorObj.info);
                        }



                        ${onError?onError:''}
                    },
                    onProgress: function(event, qId, fileObj, qData) {${onProgress?onProgress:''}},
                    onComplete: function(event, qId, fileObj, response, qData) { ${delay?"setTimeout(function(){ ${onComplete?onComplete:''} }, ${delay});" : "${onComplete?onComplete:''}" } },
                    onAllComplete: function(event, qData) {${onAllComplete?onAllComplete:''}}
                });
            }${attrs.inBoxy?'':');'}
          </script>
        """
        out << snip
        out << "</div>"
    }
}
